<?php
class AdAction extends SystemCommonAction {
    public function showList() {
        $Ad = D("Ad");
        $ad_info = $Ad->order('id desc')->select();
        $this->assign('ad_info',$ad_info);
        $this->display('index');
    }
    public function showAdd() {
        $id = intval($_GET['id']);
        if($id>0) {
            $Ad = D("Ad");
            $Ad_info = $Ad->where('id = '.$id)->find();
            if(count($Ad_info)>0) {
                $this->assign('Ad',$Ad_info);
            }
        }
         $hangye = F('haneye');
        $this->assign('hangye',$hangye);
        $this->display('showAdd');
    }
    public function add() {
        $id = intval($_POST['id']);
        $arr = array();
        $arr['title'] = $_POST['title'];
        $arr['url'] = $_POST['url'];
        $arr['type'] = $_POST['type'];
        $arr['img'] = $_POST['img'];
        $arr['url_text'] = $_POST['url_text'];
        $Ad = D("Ad");
        if($id>0) {
            $Ad_info = $Ad->where('id = '.$id)->find();
            if(count($Ad_info)>0) {
                $rs = $Ad -> where('id = '.$id) ->save($arr);
                if($rs) {
                    $this->assign('jumpUrl',__APP__.'/Admin/Ad/showList');
                    $this->success('修改成功！');
                }else {
                    $this->assign('jumpUrl',__APP__.'/Admin/Ad/showList');
                    $this->success('修改失败！');
                }
                return true;
            }
        }
        if($Ad->add($arr)) {
            $this->assign('jumpUrl',__APP__.'/Admin/Ad/showList');
            $this->error('成功！');
        }
        else {
            $this->assign('jumpUrl',__APP__.'/Admin/Ad/showList');
            $this->success('失败！');
        }


    }

    public function del() {
        $id = intval($_GET['id']);
        if($id>0) {
            $Ad = D("Ad");
            $Ad_info = $Ad->where('id = '.$id)->find();
            if(count($Ad_info)>0) {
                if($Ad->where('id = '.$id)->delete()) {
                    $this->assign('jumpUrl',__APP__.'/Admin/Ad/showList');
                    $this->error('成功！');
                }
            }
        }
        $this->assign('jumpUrl',__APP__.'/Admin/Ad/showList');
        $this->success('失败！');
    }
    
}
?>
